<script>
  import memont from 'moment'
  export default function () {
    const loadData = function (api, params) {
      return new Promise((resolve, reject) => {
        api(params)
          .then((res) => {
            resolve(res)
          })
          .catch((err) => {
            reject(err)
          })
      })
    }
    const getSearch = function (config) {
      if (config) {
        return { pageIndex: 1, pageSize: 5, ...config }
      } else {
        return { pageIndex: 1, pageSize: 5 }
      }
    }
    const format = function (search, data, config) {
      if (Array.isArray(config)) {
        if (data.value1) search[config[0]] = data.value1
        if (data.value3) search[config[2]] = data.value3
        if (data.value4) search[config[3]] = data.value4

        if (data.value2) {
          search['startDay'] = data.value2.start
            ? memont(data.value2.start).format('YYYY-MM-DD')
            : null
          search['endDay'] = data.value2.end
            ? memont(data.value2.end).format('YYYY-MM-DD')
            : null
        }
      }
      if (data.current) search['pageIndex'] = data.current
      if (data.pageSize) search['pageSize'] = data.pageSize
      if (data.orderType) search['orderType'] = data.orderType
    }
    return {
      loadData,
      getSearch,
      format,
    }
  }
</script>
